package com.example.testproj.async;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.testproj.R;

import java.util.concurrent.ThreadPoolExecutor;

public class AsyncTaskActivity extends AppCompatActivity {

    private HandlerThread handlerThread;
    private ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);
//        new MyAsyncTask().execute("任务一");
//        new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "任务二");
//        new MyAsyncTask().execute("任务三");
//        handlerThread = new HandlerThread("test");
//        handlerThread.run();
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
//        bindService(new Intent(this,MyIntentService.class),serviceConnection,BIND_AUTO_CREATE);
        startService(new Intent(this, MyIntentService.class));
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(AsyncTaskActivity.this, MyIntentService.class);
                intent.putExtra("name", "helloWorld");
                startService(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        handlerThread.quit();
        //      unbindService(serviceConnection);
    }

    public static class MyAsyncTask extends AsyncTask<String, Integer, String> {
        private static final String TAG = "MyAsyncTask";
        private String name = "";

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.d(TAG, "onPreExecute: ");
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.d(TAG, "onPostExecute: " + s);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            Log.d(TAG, "onProgressUpdate: " + name + values[0]);
        }

        @Override
        protected String doInBackground(String... strings) {
            Log.d(TAG, "doInBackground: " + strings.length);
            name = strings[0];
            for (int i = 0; i < 100; i++) {
                publishProgress(i);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return strings[0];
        }
    }
}
